package com.sihan.framework.etm.controller;

import com.framework.annotation.BusinessLog;
import com.framework.model.ApiResult;
import com.framework.model.PageResult;
import com.sihan.framework.etm.dto.WorkListDTO;
import com.sihan.framework.etm.dto.WorkTicketDTO;
import com.sihan.framework.etm.entity.WorkTicket;
import com.sihan.framework.etm.service.CommonService;
import com.sihan.framework.etm.service.EtmDeviceMountService;
import com.sihan.framework.etm.service.workTicket.WorkTicketService;
import com.sihan.framework.etm.vo.CommonQueryResultVO;
import com.sihan.framework.etm.vo.baseData.DeviceVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@Api(tags = "工作票管理")
@RestController
@RequestMapping("/system/v0/workTicket")
public class WorkTicketController {

    @Autowired
    private WorkTicketService workTicketService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private EtmDeviceMountService deviceMountService;

    @ApiOperation(value = "工作票列表", notes = "工作票列表", response = WorkTicket.class)
    @GetMapping("/list")
    @BusinessLog(value = "工作票列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "code", value = "工作票号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "查询开始检", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "查询结束时间", dataType = "string", paramType = "query")
    })
    public ApiResult<PageResult<WorkTicket>> listWorkTicket(WorkListDTO dto) {

        return workTicketService.listWorkTicket(dto);
    }


    @ApiOperation(value = "添加工作票", notes = "添加工作票", response = DeviceVO.class)
    @PostMapping("/addWorkList")
    @BusinessLog(value = "添加工作票")
    public ApiResult<?> addWorkList(@Valid @ApiParam(name = "WorkTicketDTO", value = "设备数据", required = true)
                                        @RequestBody WorkTicketDTO dto) {

        return workTicketService.addWorkList(dto);
    }


    @ApiOperation(value = "根据工作票查找详情", notes = "根据工作票查找详情", response = WorkTicketDTO.class)
    @GetMapping("/findByTicketCode")
    @BusinessLog(value = "根据工作票查找详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ticketCode", value = "工作票号", dataType = "string", paramType = "query")
    })
    public ApiResult<?> findByTicketCode(String ticketCode) {

        return workTicketService.findByTicketCode(ticketCode);
    }


    @ApiOperation(value = "修改工作票状态")
    @PostMapping("/updateTowerCode")
    @BusinessLog(value = "修改工作票状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "towerCode", value = "工作票号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "state", value = "工作状态 0未开始 1进行中 2结束", dataType = "int", paramType = "query", required = true)
    })
    public ApiResult<?> updateTowerCode(String towerCode,Integer state) {

        return workTicketService.updateTowerCode(towerCode,state);
    }

    @GetMapping("/query/devPull")
    @ApiOperation(value = "未绑定设备下拉", response = CommonQueryResultVO.class)
    public ApiResult<List<CommonQueryResultVO>> unboundDevice() {

        return ApiResult.success(commonService.unboundDevice());
    }


    @ApiOperation("归还工器具")
    @GetMapping("/query//giveBack")
    @BusinessLog(value = "归还工器具")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备Id", dataType = "string", required = true),
            @ApiImplicitParam(name = "taskCode", value = "工作票号", dataType = "string", required = true)
    })
    public ApiResult<?> giveBack(String deviceId,String taskCode) {


        return deviceMountService.giveBack(deviceId,taskCode);
    }

}
